package org.jeecg.modules.stCaigd.service.impl;

import org.jeecg.modules.stCaigd.entity.HqStCaigd;
import org.jeecg.modules.stCaigd.entity.HqStCaigdDetail;
import org.jeecg.modules.stCaigd.mapper.HqStCaigdDetailMapper;
import org.jeecg.modules.stCaigd.mapper.HqStCaigdMapper;
import org.jeecg.modules.stCaigd.service.IHqStCaigdService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 食堂采购单
 * @Author: jeecg-boot
 * @Date:   2020-08-09
 * @Version: V1.0
 */
@Service
public class HqStCaigdServiceImpl extends ServiceImpl<HqStCaigdMapper, HqStCaigd> implements IHqStCaigdService {

	@Autowired
	private HqStCaigdMapper hqStCaigdMapper;
	@Autowired
	private HqStCaigdDetailMapper hqStCaigdDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(HqStCaigd hqStCaigd, List<HqStCaigdDetail> hqStCaigdDetailList) {
		hqStCaigdMapper.insert(hqStCaigd);
		if(hqStCaigdDetailList!=null && hqStCaigdDetailList.size()>0) {
			for(HqStCaigdDetail entity:hqStCaigdDetailList) {
				//外键设置
				entity.setStCaigdId(hqStCaigd.getId());
				hqStCaigdDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqStCaigd hqStCaigd,List<HqStCaigdDetail> hqStCaigdDetailList) {
		hqStCaigdMapper.updateById(hqStCaigd);
		
		//1.先删除子表数据
		hqStCaigdDetailMapper.deleteByMainId(hqStCaigd.getId());
		
		//2.子表数据重新插入
		if(hqStCaigdDetailList!=null && hqStCaigdDetailList.size()>0) {
			for(HqStCaigdDetail entity:hqStCaigdDetailList) {
				//外键设置
				entity.setStCaigdId(hqStCaigd.getId());
				hqStCaigdDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqStCaigdDetailMapper.deleteByMainId(id);
		hqStCaigdMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqStCaigdDetailMapper.deleteByMainId(id.toString());
			hqStCaigdMapper.deleteById(id);
		}
	}
	
}
